<?php
namespace Admin\Controller;
use Think\Controller;


class JigouController extends Controller {

    public function jigouadd(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        if (!IS_POST) {
            $this->display();
        }else{
            $jigou=M('jigou');
            $_POST['is_del']=0;
            $_POST['ctime']=time();
            $_POST['utime']=time();
            if ($jigou->add($_POST)) {
                $this->success("添加成功",(U('Admin/jigou/jigoulist')));
            }else{
                $this->error("添加失败");
            }
        }
    }
    public function jigoulist(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $jigou = M('jigou');

        $count = $jigou->where(array('is_del' => '0'))->count();
        $Page  = new \Think\Page($count,10);
        $jigou = $jigou->where('is_del=0')->order('ctime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $show = $Page->show();

        $this->assign('page',$show);
        $this->assign('rows',$jigou); 
        $this->display();
    }

    public function jigouedit(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $jigouModel = D('jigou');
    	if (!IS_POST) {
            $jigou_id = I('aid');
            $jigou  = $jigouModel->find($jigou_id); 
            $this->assign('jigou',$jigou); 
            $this->display();
    	}else{
            $jigouModel->title=$_POST['title']; 
            $jigouModel->code=$_POST['code']; 
            $jigouModel->allowcode=$_POST['allowcode']; 
            $jigouModel->margincode=$_POST['margincode']; 
            $jigouModel->utime=time();
            if ($jigouModel->where('aid='.$_POST['aid'])->save()) {
                $this->success("修改成功",(U('Admin/jigou/jigoulist')));
            }else{
                $this->error("修改失败");
            }
        }
    }
    public function jigoudel(){
        if (!checkCookie()) {
            $this->redirect('Admin/Index/login');
        }
        $jigou = M('jigou');
        $jigou->is_del = 1;
        if ($jigou->where('aid='.$_GET['aid'])->save()) {
            $this->success("删除成功",(U('Admin/jigou/jigoulist')));
        } else {
            $this->error("删除失败");
        }
    }
}